// 创建一个群，保存通知，通知变化之后通知每个家长（触发所有观察者对象）
class Group {
  constructor () {
    this.message = ''
    this.parents = []
  }

  getMessage () {
    return this.message
  }

  sendMessage () {
    this.parents.forEach(parent => {
      parent.update()
    })
  }

  setMessage (message) {
    this.message = message
    this.sendMessage()
  }

  attach (parent) {
    this.parents.push(parent)
  }
}

class Parent {
  constructor (name, group) {
    this.name = name
    this.group = group
    this.group.attach(this)
  }

  update() {
    console.log(`${this.name} 收到通知: ${this.group.getMessage()}`);
  }
}

// let group = new Group()
// let t1 = new Parent('李妈妈', group)
// let t2 = new Parent('王妈妈', group)
// let t3 = new Parent('张妈妈', group)

// group.setMessage('开家长会')
// group.setMessage('开运动会')


// 定义几个策略
const PaymentMethodStratedgy = {
  BankAccount: function (money) {
    return money > 50 ? money * 0.7 : money
  },
  CreditCard: function (money) {
    return money * 0.8
  },
  Alipay: function (money) {
    return money
  }
}
const userPay = function (selectedStrategy, money) {
  return PaymentMethodStratedgy[selectedStrategy](money)
}

console.log('银行卡支付价格为：' + userPay('BankAccount', 100)); //70
console.log('支付宝支付价格为：' + userPay('Alipay', 100)); //100
console.log('信用卡支付价格为：' + userPay('CreditCard', 100)); //80